package com.caineng.zhaobiao.zbapi.utils.email;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.caineng.zhaobiao.zbapi.utils.redis.CainengRedisUtils;
import com.caineng.zhaobiao.zbapi.utils.redis.RedisKeyConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.util.concurrent.TimeUnit;

/**
 * 邮件发送工具类
 */
@Component
public class MyEmailUtil {

    @Value("${spring.mail.username}")
    private String MAIL_SENDER; //邮件发送者

    @Value("${spring.mail.personname}")
    private String personname ;//邮件显示中文名

    @Autowired
    CainengRedisUtils cainengRedisUtils;


    @Autowired
    private JavaMailSender javaMailSender;//注入QQ发送邮件的bean

    private Logger logger = LoggerFactory.getLogger(MyEmailUtil.class);

    /**
     * 发送文本邮件
     */
    public boolean sendSimpleMail(String subject, String content) {
        try {
            SimpleMailMessage mailMessage= new SimpleMailMessage();
            mailMessage.setFrom(MAIL_SENDER);//发送者
            mailMessage.setTo("2840402853@qq.com");//接收者
            mailMessage.setSubject(subject);//邮件标题
            mailMessage.setText(content);//邮件内容
            javaMailSender.send(mailMessage);//发送邮箱
            return true ;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("邮件发送失败", e.getMessage());

            return false ;
        }
    }

    /**
     * 发送文本邮件
     *
     * @param mailBean
     */
    public boolean sendSimpleMail(MyEmailBean mailBean) {
        try {
            SimpleMailMessage mailMessage= new SimpleMailMessage();
            mailMessage.setFrom(MAIL_SENDER);//发送者
            mailMessage.setTo(mailBean.getRecipient());//接收者
            mailMessage.setSubject(mailBean.getSubject());//邮件标题
            mailMessage.setText(mailBean.getContent());//邮件内容
            javaMailSender.send(mailMessage);//发送邮箱
            return true ;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("邮件发送失败", e.getMessage());
            return false ;
        }
    }

    /**
     * 发送HTML模板
     * @param mailBean
     */
    public boolean sendHTMLMail(MyEmailBean mailBean) {
        MimeMessage mimeMailMessage = null;
        try {
            mimeMailMessage = javaMailSender.createMimeMessage();
            //true 表示需要创建一个multipart message
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(MAIL_SENDER,personname);//发送者

            mimeMessageHelper.setTo(mailBean.getRecipient());//接受者
            mimeMessageHelper.setSubject(mailBean.getSubject());//邮件标题
            //这里的 true，你加了的话，它发送你HTML页面里面的内容
            //不加的话，默认是 false，它发送整个HTML页面代码
            mimeMessageHelper.setText(mailBean.getContent(), true);
            //邮件抄送
            javaMailSender.send(mimeMailMessage);//发送邮件
            return true ;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("邮件发送失败", e.getMessage());
            return false ;
        }
    }

    /**
     * 发送HTML模板
     * @param mailBean
     */
    public boolean sendHTMLEmail(MyEmailBean mailBean) {
        MimeMessage mimeMailMessage = null;
        try {
            mimeMailMessage = javaMailSender.createMimeMessage();
            //true 表示需要创建一个multipart message
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(MAIL_SENDER,personname);//发送者

            mimeMessageHelper.setTo(mailBean.getRecipient());//接受者
            mimeMessageHelper.setSubject(mailBean.getSubject());//邮件标题
            //这里的 true，你加了的话，它发送你HTML页面里面的内容
            //不加的话，默认是 false，它发送整个HTML页面代码
            mimeMessageHelper.setText(mailBean.getContent(), true);
            //邮件抄送
            javaMailSender.send(mimeMailMessage);//发送邮件
            return true ;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("邮件发送失败", e.getMessage());
            return false ;
        }
    }

    /**
     * 发送邮箱验证码
     * @param email
     * @return
     */
    public boolean sendEmailCode(String email){
        String code = RandomUtil.randomNumbers(6);
        logger.info(email+"邮箱验证码是"+code);
        //5分钟有效
        cainengRedisUtils.setCacheObject(RedisKeyConstant.KEY_PREFIX+email+"_SMS",code,5L, TimeUnit.MINUTES);
        MyEmailBean bean = new MyEmailBean();
        bean.setRecipient(email);
        bean.setSubject("朋利招标邮箱验证码");
        String content = "<h3>尊敬的朋利招标用户，感谢您使用朋利招投标服务!全面、精准、便捷的招投标信息查询平台， 招投标查询，招标定制推送，企业分析监测，让您随时随地掌握商机。请搜索小程序或者下载APP:朋利招标</h3>";
        content += " <h4>您的邮箱验证码为: <h2>"+code+"</h2>如果这不是您自己发送的验证码，可能是注册用户填错了邮箱地址，请忽略该邮件！</h4>" ;
        bean.setContent(content);
        return sendHTMLEmail(bean) ;

    }

    /**
     * 校验邮箱验证码
     * @param email
     * @param code
     * @return
     */
    public boolean checkEmailCode(String email,String code){
        String codeRedis = cainengRedisUtils.getCacheObject(RedisKeyConstant.KEY_PREFIX+email+"_SMS");
        if(StrUtil.equals(codeRedis,code)){
            cainengRedisUtils.deleteObject(RedisKeyConstant.KEY_PREFIX+email+"_SMS");
            return true;
        }
        return false ;
    }

    public static void main(String[] args) {
        //sendEmailCode("2840402853@qq.com")

    }
}
